<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

Class Customer_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function insert_user($data = array()) {
        $table = "customer";
        if ($data != '') {
            $val = array(
                'cm_code' => $data['cm_code'],
                'cm_lineid' => $data['cm_lineid'],
                'cm_titlename' => $data['cm_titlename'],
                'cm_type' => $data['cm_type'],
                'cm_fname' => $data['cm_fname'],
                'cm_lname' => $data['cm_lname'],
                'cm_idcard' => $data['cm_idcard'],
                'cm_package' => $data['cm_package'],
                'cm_tel' => $data['cm_tel'],
                'cm_email' => $data['cm_email'],
                'cm_addr' => $data['cm_addr'],
                'cm_tambon' => $data['cm_tambon'],
                'cm_amphor' => $data['cm_amphor'],
                'cm_province' => $data['cm_province']
            );

            $rs = $this->select_by_filter($table, $val['cm_id']);
            $is = (!empty($rs) && $rs > 0) ? FALSE : ($this->db->insert($table, $val) ? $this->db->insert_id() : FALSE);
            return $is;
        }
    }

    function check_insert($table, $val) {
        $rs = $this->db->where('cm_id', $val)->get($table);
        $count = $rs->num_rows();
        return $count;
    }

    function select_by_table($table) {
        $query = $this->db->get($table)->result();
        return $query;
    }

    function select_by_filter($table, $id) {
        $rs = $this->db->where('cm_id', $id)->get($table)->row_array();
        return $rs;
    }

    function update_user($data = array()) {
        $table = "customer";
        if ($data != '') {
            $val = array(
                'cm_code' => $data['cm_code'],
                'cm_lineid' => $data['cm_lineid'],
                'cm_titlename' => $data['cm_titlename'],
                'cm_type' => $data['cm_type'],
                'cm_package' => $data['cm_package'],
                'cm_fname' => $data['cm_fname'],
                'cm_lname' => $data['cm_lname'],
                'cm_idcard' => $data['cm_idcard'],
                'cm_tel' => $data['cm_tel'],
                'cm_email' => $data['cm_email'],
                'cm_addr' => $data['cm_addr'],
                'cm_tambon' => $data['cm_tambon'],
                'cm_amphor' => $data['cm_amphor'],
                'cm_province' => $data['cm_province']
            );
            $is = ($this->db->where('cm_id', $data['cm_id'])->update($table, $val)) ? TRUE : FALSE;
            return $is;
        }
    }

    function delete_user($table, $data) {
        ($this->db->where('cm_id', $data)->delete($table)) ? $is = TRUE : $is = FALSE;
        return $is;
    }

}
